package com.blb.controller;

import com.blb.entity.Company;
import com.blb.entity.Staff;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IAccountService;
import com.blb.utils.ResponseHelp;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/account")

public class AccountController {

    @Autowired
    private IAccountService accountService;

    /**
     * 跳页面 权限认证
     * @param page
     * @return
     */
    @RequestMapping("{page}")
    @RequiresPermissions("corporateaccount")
    public String account(@PathVariable String page){
        return"corporateaccount/"+page;
    }


    /**
     *  企业账号管理分页和模糊查询
     * @param legal
     * @param companyName
     * @param companyStatus
     * @param creatTime
     * @param creatTime1
     * @param start
     * @param length
     * @return
     */
    @ResponseBody
    @PostMapping("page")
    public BlbPage page(String legal, String companyName, String companyStatus, String creatTime, String creatTime1, int start, int length){

        //查询分页数据
        List<Company> company = accountService.getCompanys(start,length,legal,companyName,companyStatus, creatTime, creatTime1);
        int count = accountService.count(legal, companyName, companyStatus, creatTime,creatTime1);
        return new DataTablePage(count, count, company);
    }

    /**
     * 根据企业id实现员工的分页和模糊查询
     * @param request
     * @param start
     * @param length
     * @param name
     * @param accountStatus
     * @param entryTime
     * @param entryTime1
     * @return
     */
    @ResponseBody
    @PostMapping("page1")
    public BlbPage page1(HttpSession request,int start, int length, String name, String accountStatus, String entryTime, String entryTime1){

        String id = (String)request.getAttribute("id");
        //查询分页数据
        List<User> staff = accountService.getStaffs(id,start,length, name, accountStatus, entryTime, entryTime1);
        int count = accountService.count1(id,name, accountStatus, entryTime, entryTime1);
        return new DataTablePage(count, count, staff);
    }

    /**
     * 通过企业id找到员工
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("findStaff")
    public String findStaff(String id, HttpSession request){
        request.setAttribute("id",id);
        return "corporateaccount/wt_qiyeyonghu_zi.html";
    }

    /**
     * 通过员工id找到企业和员工信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("findAll")
    public String findAll(String id, Model model){
        User user = accountService.getCompayAndStaff(id);
        model.addAttribute("user",user);
        return "corporateaccount/wt_YH_xiangxi.html";
    }

    /**
     * 修改企业账号状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("updateStatus")
    @ResponseBody
    public ResponseHelp updateStatus(String id, String status){
        accountService.updateStatus(id, status);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;

    }

    /**
     * 批量修改企业账号状态
     * @param ids
     * @return
     */
    @RequestMapping("updateAllStatus")
    @ResponseBody
    public ResponseHelp updateAllStatus(@RequestBody(required = false) String[] ids){
        accountService.updateAllStatus(ids);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;

    }

}
